//
//  QYScanSucceedViewController.m
//  QingYue
//
//  Created by Lei_Zhong_Xun on 2017/5/25.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYScanSucceedViewController.h"
#import "QYBookDetailListHeaderView.h"
#import "QYBookDetailModel.h"

@interface QYScanSucceedViewController ()
@property (strong, nonatomic) QYBookDetailListHeaderView *listHeader; // 头视图
/** 灰色线*/
@property (nonatomic,strong) UIView* grayView;

/** 借阅按钮*/
@property (nonatomic,strong) UIButton* borrowButton;
/** 归还按钮*/
@property (nonatomic,strong) UIButton* returnButton;
/** 转接按钮*/
@property (nonatomic,strong) UIButton* subtenantButton;
/** 图书状态*/
@property (nonatomic,copy) NSString* bookState;
/** 扫描详情model*/
@property (nonatomic,strong) QYBookDetailModel* detailModel;


@end

@implementation QYScanSucceedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫描详情";
    [self.view addSubview:self.listHeader];
    [self.view addSubview:self.grayView];
    [self.view addSubview:self.borrowButton];
    [self.view addSubview:self.returnButton];
    [self.view addSubview:self.subtenantButton];
    [self loadData];
    [self setupNavigationItem];
}

- (void)setupNavigationItem {
    UIButton *left_Button = [[UIButton alloc] init];
    [left_Button setTitle:@"back" forState:UIControlStateNormal];
    [left_Button setTitleColor:[UIColor colorWithRed: 21/ 255.0f green: 126/ 255.0f blue: 251/ 255.0f alpha:1.0] forState:(UIControlStateNormal)];
    [left_Button sizeToFit];
    [left_Button addTarget:self action:@selector(left_BarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left_BarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left_Button];
    self.navigationItem.leftBarButtonItem = left_BarButtonItem;
    
}

- (void)left_BarButtonItemAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 响应事件
//借阅
- (void)completeButtonAction1
{
    [SVProgressHUD showWithStatus:@"正在借阅"];
    
    NSDictionary *param = @{@"id" : _jump_bar_code,@"username":[kUserDefaults objectForKey:@"username"]};
    [[YCRequestDataService sharedManager] Post:@"Borrow" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [SVProgressHUD dismissWithDelay:0.35];
//            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

//归还
- (void)completeButtonAction2
{
    [SVProgressHUD showWithStatus:@"正在归还"];
    
    NSDictionary *param = @{@"id" : _jump_bar_code,@"username":[kUserDefaults objectForKey:@"username"]};
    [[YCRequestDataService sharedManager] Post:@"PreReturn" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [SVProgressHUD dismissWithDelay:0.35];
//            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

//转借
- (void)completeButtonAction3
{
    [SVProgressHUD showWithStatus:@"正在转借"];
    
    NSDictionary *param = @{@"id" : _jump_bar_code,@"username":[kUserDefaults objectForKey:@"username"]};
    
    [[YCRequestDataService sharedManager] Post:@"Deliver" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [SVProgressHUD dismissWithDelay:0.35];
//            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}
#pragma mark - 数据加载
- (void)loadData
{
    [SVProgressHUD show];
    [[YCRequestDataService sharedManager] Post:@"GetBook" parameters:@{@"id" : _jump_bar_code} success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"扫描成功"]) {
            [SVProgressHUD dismissWithDelay:0.35];
            _detailModel = [[QYBookDetailModel alloc] initWithDataDic:response];
            _listHeader.model = _detailModel;
            [self judgeBookeStateWithString:response[@"Status"]];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});

    }];
}

//判断图书状态,按钮状态
- (void)judgeBookeStateWithString: (NSString *)Str
{
    if ([Str isEqualToString:@"0"]) {
        _borrowButton.enabled = YES;
        _borrowButton.backgroundColor = MyGreenColor;
        _returnButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _subtenantButton.enabled = NO;
        _subtenantButton.backgroundColor = RGB(231, 232, 233, 1.0);

    }
    else if ([Str isEqualToString:@"1"]){
        _borrowButton.enabled = NO;
        _borrowButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _returnButton.enabled = YES;
        _returnButton.backgroundColor = MyGreenColor;
        _subtenantButton.enabled = YES;
        _subtenantButton.backgroundColor = MyGreenColor;
    }
    else if ([Str isEqualToString:@"2"]){
        _borrowButton.enabled = NO;
        _borrowButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _returnButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _subtenantButton.enabled = NO;
        _subtenantButton.backgroundColor = RGB(231, 232, 233, 1.0);
    }
    else if ([Str isEqualToString:@"3"]){
        _borrowButton.enabled = NO;
        _borrowButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _returnButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _subtenantButton.enabled = NO;
        _subtenantButton.backgroundColor = RGB(231, 232, 233, 1.0);
    }
    else if ([Str isEqualToString:@"4"]){
        self.borrowButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _returnButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _subtenantButton.enabled = NO;
        _subtenantButton.backgroundColor = RGB(231, 232, 233, 1.0);
    }
    else {
        _borrowButton.enabled = NO;
        _borrowButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _returnButton.enabled = NO;
        _returnButton.backgroundColor = RGB(231, 232, 233, 1.0);
        _subtenantButton.enabled = NO;
        _subtenantButton.backgroundColor = RGB(231, 232, 233, 1.0);
    }
}

#pragma mark - 布局
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [_listHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@200);
    }];
    [_grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_listHeader.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@0.5);
        
    }];
    [_borrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_grayView.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.right.equalTo(self.view.mas_right).offset(- 30);
        make.height.equalTo(@50);
    }];
    [_returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_borrowButton.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.right.equalTo(self.view.mas_right).offset(- 30);
        make.height.equalTo(@50);

    }];
    [_subtenantButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_returnButton.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.right.equalTo(self.view.mas_right).offset(- 30);
        make.height.equalTo(@50);

    }];
}

#pragma mark - lazy load
- (QYBookDetailListHeaderView *)listHeader
{
    if (!_listHeader) {
        __weak typeof(self) weakSelf = self;
        _listHeader = [[QYBookDetailListHeaderView alloc] init];
        _listHeader.focusBtn.hidden = YES;
        _listHeader.backgroundColor = [UIColor whiteColor];
        _listHeader.Lblock = ^(NSString *isbn, NSString *api) {
            NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"],
                                    @"isbn":isbn};
            [weakSelf focusRequestWithParam:param withAPI:api];
        };
    }
    return _listHeader;
}

- (void)focusRequestWithParam:(NSDictionary *)param withAPI:(NSString *)api {
    
    [[YCRequestDataService sharedManager] Post:api parameters:param success:^(id response) {
        [self loadData];
    } failure:^{
        
    }];
}

- (UIView *)grayView
{
    if (!_grayView) {
        _grayView = [[UIView alloc] init];
        _grayView.backgroundColor = [UIColor lightGrayColor];
    }
    return _grayView;
}
- (UIButton *)borrowButton
{
    if (!_borrowButton) {
        _borrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _borrowButton.layer.cornerRadius = 5;
        [_borrowButton setTitle:@"借阅" forState:UIControlStateNormal];
        [_borrowButton addTarget:self action:@selector(completeButtonAction1) forControlEvents:UIControlEventTouchUpInside];

    }
    return _borrowButton;
}
- (UIButton *)returnButton
{
    if (!_returnButton) {
        _returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _returnButton.layer.cornerRadius = 5;
        [_returnButton setTitle:@"归还" forState:UIControlStateNormal];
        [_returnButton addTarget:self action:@selector(completeButtonAction2) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _returnButton;
}
- (UIButton *)subtenantButton
{
    if (!_subtenantButton) {
        _subtenantButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _subtenantButton.layer.cornerRadius = 5;
        [_subtenantButton setTitle:@"转借" forState:UIControlStateNormal];
        [_subtenantButton addTarget:self action:@selector(completeButtonAction3) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _subtenantButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
